package com.uchengwang.ui.cm_ui.me.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/1/22.
 * 社圈
 */

public class CirclePageAdapter extends RecyclerView.Adapter<CirclePageAdapter.PageViewHolder>{
    private Context context;
    private List<CycleBean> list = new ArrayList<>();

    public CirclePageAdapter(Context context){
        this.context  = context;
    }

    public void setData(List<CycleBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<CycleBean> getData(){
        return list;
    }

    @Override
    public PageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_main_item, parent, false);
        return new PageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PageViewHolder holder, int position) {
        final CycleBean bean = list.get(position);
        if (!TextUtils.isEmpty(bean.getCycleCover())){
            ImageManager.getInstance().loadImage(context, holder.page_img, bean.getCycleCover(), ImageManager.ImageType.ROUND);
        }else {
            ImageManager.getInstance().loadImage(context, holder.page_img, R.drawable.default_image, ImageManager.ImageType.ROUND);
        }
        holder.page_text.setText(bean.getCycleName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/uCity/view/socialCircleDetail/socialCircle"+ bean.getId()+".html"  +"?isopen=true");
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class PageViewHolder extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView page_img;
        private TextView page_text;
        public PageViewHolder(View view) {
            super(view);
            AutoUtils.autoSize(view);

            itemView = view;
            page_img = (ImageView) view.findViewById(R.id.page_img);
            page_text = (TextView) view.findViewById(R.id.page_text);
        }
    }
}
